<template>
  <view v-if="showPopup" class="content" @click.stop.prevent="onTouchBg"  @touchmove.stop.prevent=''> 
    <view class="mask"></view>
    <view class="dialog-wrap">
      <view class="dialog anim-scale" @click.stop.prevent="onTouchDialog">
        <view class="dialog-top">
          <text class="dialog-top-title">{{title}}</text>
          <view class="dialog-top-close" @click="onBtnClose">
            <text>{{'\ue723'}}</text>
          </view>
        </view>
        <view class="dialog-bot">
          <view class="dialog-notes">{{desc}}</view>
          <view v-if="isLoading" class="progress-wrap">
            <progress class="progress" stroke-width="10" :percent="percent" :show-info='true' activeColor="#10AEFF" backgroundColor="#F6F7F8"/>
            <view class="progress-tips">
              <text class="progress-desc">{{stateText}}</text>
            </view>
          </view>
          <view v-else class="btn-wrap">
            <view v-if="showCancel" class="btn-cancel" @click="onBtnCancel">取消</view>
            <view class="btn-confirm" @click="onBtnConfirm">
              <text>{{confirmText}}</text>
              <text class="countdown-text">（{{countDown}} s）</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import hotupdateMgr from './hotupdateMgr'
  
  var cdTimerId = null 
  
  export default {
    data() {
      return {
        showPopup: false,
        isLoading: false,
        showCancel: false,
        confirmText: '确定',
        stateText: '',
        countDown: 5,
        state: 'none',  // 'none' | 'hasNew' | 'newest' | 'fail' | 'progress' 
        title: '版本更新',
        desc: '',
        dataClickOutsideToClose: false, 
        releaseNotes: '1、修复已知问题；\n2、优化用户体验；',
        percent: 0,
        progressText: '', //'(100.08/240.17M)', 
      }
    },
    mounted() {
      console.log('=== mounted') 
    },

    methods: {
      open: function(param){
        if (param) {
          this.initData(param) 
        }
        this.showPopup = true 
        this.showCountDown() 
      }, 
      close: function(){ 
        this.showPopup = false 
      },
      initData(param) {
        this.state = param.state 
        this.desc  = param.desc 
        
        this.showCancel  = this.state==='hasNew' 
        this.confirmText = this.state==='hasNew' ? '立即更新' : '确定' 
        
        this.isLoading = false 
        
      },
      onTouchBg: function(e) {
        e.stopPropagation() //取消冒泡 
        if (this.dataClickOutsideToClose) {
          this.close() 
        }
      },
      onTouchDialog: function(e) {
        e.stopPropagation() //取消冒泡 
      },
      onBtnCancel() {
        this.close()
      },
      onBtnConfirm() {
        console.log('=== onBtnConfirm')
        if (this.state === 'hasNew') {
          hotupdateMgr.startUpdate() 
          this.percent   = 0 
          this.isLoading = true 
        } else {
          this.close() 
        } 
      },
      onBtnClose() {
        hotupdateMgr.stopUpdate() 
        this.close() 
      },
      updateProgress(percent) {
        this.isLoading = true 
        this.percent   = Math.min( 100, percent) 
        this.stateText = this.percent < 100 ? '正在下载更新，请稍后' : '即将进行安装，请勿退出！' 
      },
      showCountDown() {
        var self = this 
        this.countDown = 5 
        cdTimerId = setInterval(()=>{
          if (self.countDown > 0) {
            self.countDown-- 
          } else {
            self.stopCountDown() 
            self.onBtnConfirm() 
          }
        }, 1000) 
      },
      stopCountDown() {
        if (cdTimerId) {
          clearInterval(cdTimerId) 
          cdTimerId = null 
        }
      },
    }
  }
</script>

<style scoped>
  @font-face {
    font-family: "iconfont";
    src: url('@/static/font/iconfont.ttf');
  }
  .content {
    width: 100vw;
    height: 100vh;
    position: fixed;
    left: 0;
    bottom: 0; 
    z-index: 999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .mask { 
    width: 100vw;
    height: 100vh;
    position: fixed; 
    left: 0; 
    bottom: 0; 
    background-color: rgba(0, 0, 0, 0.4); 
    transition: opacity 0.3 ease; 
    transform-origin: 50% 50%; 
  } 
  .dialog-wrap {
    position: fixed; 
    width: 100vw;
    height: 100vh;
    left: 0; 
    bottom: 0; 
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  } 
  .dialog {
    width: 300px;
    border-radius: 6px;
   
    background-color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .dialog-top {
    width: 100%;
    height: 50px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    position: relative;
  }
  .dialog-top-title {
    font-size: 18px;
    font-weight: bold;
    color: #4B73FE;
  }
  .dialog-top-close {
    font-family: "iconfont";
    font-size: 16px;
    color: #333;
    position: absolute;
    right: 0;
    top: 0;
    padding: 6px;
  }
  .dialog-bot {
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  @keyframes pop-scale{
    0% {transform: scale(0.4);}
    100% {transform: scale(1);}
  }
  .anim-scale {
    animation-name: pop-scale;
    animation-duration: 0.3s;
  }
  .dialog-notes {
    width: 100%;
    height: 60px;
    line-height: 60px;
    font-size: 15px;
    font-weight: 400;
    color: #333333;
    white-space: pre-line;  /*合并空白符序列，但是保留换行符。*/
  }
  .progress-wrap {
    width: 100%;
    height: 50px;
    padding-bottom: 20px;
  }
  .progress {
    width: 100%;
    height: 35px;
  }
  .progress-tips {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .progress-desc {
    font-size: 11px;
    font-weight: 400;
    color: #666;
  }
  .btn-wrap {
    width: 100%;
    height: 44px;
    border-top:1px solid #EFEFF4;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .btn-cancel {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    font-size: 15px;
    color: #333;
    border-right: 1px solid #E5E5E5;
  }
  .btn-confirm {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    font-size: 15px;
    color: #4B73FE;
  }
  .countdown-text {
    font-size: 15px;
    color: #4B73FE;
  }
  /* 设置圆角 这主要是加上deep才能改变它原来的样式  */
  /* #ifndef APP-NVUE */
  /deep/ .uni-progress-bar {
    border-radius: 20px !important;
    overflow: hidden;
  }
  /* #endif */
</style>
